package syn;

/**
 * @Author: zhanghuan
 * @date: 2020/10/2 15:42
 * @description:
 */
public class UnsafeBuyTicket {

    public static void main(String[] args) {
        BuyTicket buyTicket = new BuyTicket();
        new Thread(buyTicket).start();
        new Thread(buyTicket).start();
        new Thread(buyTicket).start();
    }

}

class BuyTicket implements Runnable {
   volatile int ticket = 20;
    boolean flag = true;


    @Override
    public void run() {

        while (flag) {
            if (ticket <= 0) {
                flag = false;
                return;
            }
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + "----------->" + ticket--);
        }

    }
}
